package com.iplant.basic.controller.bfc;

import com.alibaba.fastjson.JSON;
import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.TraceLogInfo;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.annotation.trace.TraceModuleAPI;
import com.iplant.base.utils.general.DesUtil;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bfc.auth.BFCAuthRecord;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

@RestController
@RequestMapping("/api/BFCAuthRecord")
@TraceModuleAPI("授权管理")
@Slf4j
public class BFCAuthRecordController extends BaseController {


    public BFCAuthRecordController() {

    }

    /**
     * 条件查询
     */
    @TraceAPI("授权导入")
    @PostMapping("/AuthImport")
    public Object AuthImport(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        String wMsg = "";
        try {
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BFCAuthRecord wBFCAuthRecord = JSON.parseObject(DesUtil.decrypt(
                    StringUtils.parseString(wParam.get("data"))), BFCAuthRecord.class);

            //对比授权文件与获取的授权文件是否一致

            BFCAuthRecord wAuthRecord = BFCAuthRecord.GetDefaultValue();
            wAuthRecord.FullAuthRecord();

            if (!wBFCAuthRecord.Compare(wAuthRecord, Constants.AuthCompareType)) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "授权文件不匹配，请重新申请后再授权！");
                return wResult;
            }

            if (wBFCAuthRecord.ExpireDate.compareTo(Calendar.getInstance()) <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "授权文件授权时间已过，请重新申请后再授权！");
                return wResult;
            }

            Constants.SetAuthRecord(wBFCAuthRecord);

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wAuthRecord);


        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /// <summary>
    /// 授权申请文件下载
    /// </summary>
    /// <returns></returns>

    @TraceAPI("授权申请文件导出")
    @GetMapping("/AuthExport")
    public Object AuthExport(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            String wCompanyName = StringUtils.parseString(request.getParameter("CompanyName"));
            String wProjectName = StringUtils.parseString(request.getParameter("ProjectName"));
            String wProgramName = StringUtils.parseString(request.getParameter("ProgramName"));
            String wProgramVersion = StringUtils.parseString(request.getParameter("ProgramVersion"));


            String wFilePath = StringUtils.CombinePath(Constants.getStaticPath(), Constants.Client_Upload_Save_Path,
                    "/AuthFiles/授权申请文件.txt");

            File wFile = new File(wFilePath);

            if (!wFile.exists()) {
                wFile.mkdirs();
                if (wFile.exists()) {
                    wFile.delete();
                }
            } else {
                wFile.delete();
            }

            BFCAuthRecord wBFCAuthRecord = BFCAuthRecord.GetDefaultValue(wCompanyName, wProjectName, wProgramName,
                    wProgramVersion);

            wBFCAuthRecord.FullAuthRecord();


            Files.write(wFile.toPath(), StringUtils.parseListArgs(DesUtil.encrypt(JSON.toJSONString(wBFCAuthRecord))));


            String wFileRelative = StringUtils.CombinePath(GetProjectName(request), Constants.Client_Upload_Save_Path
                    , "/AuthFiles/授权申请文件.txt");


            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wFileRelative, wBFCAuthRecord);
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @TraceAPI("授权信息")
    @GetMapping("/Info")
    public Object Info() {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, Constants.GetAuthRecord());

        } catch (
                Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
